package com.xtianzhuang.www.controller.blindDate;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.xtianzhuang.www.controller.BaseController;
import com.xtianzhuang.www.entity.VUserMarriage;
import com.xtianzhuang.www.entity.VUserMarriageExample;
import com.xtianzhuang.www.entity.constants.CodeConstants;
import com.xtianzhuang.www.entity.constants.IsDeletedConstants;
import com.xtianzhuang.www.entity.request.DeletePersonRequest;
import com.xtianzhuang.www.entity.request.PersonListQueryRequest;
import com.xtianzhuang.www.entity.request.SavePersonRequest;
import com.xtianzhuang.www.entity.request.base.BaseRequest;
import com.xtianzhuang.www.entity.response.DeleteStoryBudgetResponse;
import com.xtianzhuang.www.entity.response.PersonListQueryResponse;
import com.xtianzhuang.www.entity.response.SaveUserResponse;
import com.xtianzhuang.www.entity.response.UploadPictureResponse;
import com.xtianzhuang.www.entity.response.base.BaseResponse;
import com.xtianzhuang.www.utils.PageData;

@Controller
@RequestMapping("/blinddate")
public class BlinddateController extends BaseController {
	@RequestMapping(value = "/addPerson")
	public ModelAndView addAccount(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		modelAndView.setViewName("show/person-add");
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/ajaxSavePerson")
	@ResponseBody
	public SaveUserResponse saveUser(HttpServletRequest request, SavePersonRequest savePersonRequest) {
		SaveUserResponse response = new SaveUserResponse();
		try {
			blindDateServiceBiz.savePerson(request, savePersonRequest);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}

	@RequestMapping(value = "/uploadPicture")
	@ResponseBody
	public UploadPictureResponse uploadPicture(MultipartFile file, HttpServletRequest request) {
		UploadPictureResponse response = new UploadPictureResponse();
		try {
			blindDateServiceBiz.checkFile(file);
			// 准备文件夹,获取项目中upload文件夹的路径
			String parentDir = request.getServletContext().getRealPath("upload");
			blindDateServiceBiz.checkDir(parentDir);
			// 获取原始文件名
			String originalFilename = file.getOriginalFilename();
			logger.info("\t originalFilename=" + originalFilename);
			// 确定最终保存时使用的文件
			String filename = UUID.randomUUID().toString().replace("-", "");
			String suffix = "";
			int beginIndex = originalFilename.lastIndexOf(".");
			if (beginIndex != -1) {
				suffix = originalFilename.substring(beginIndex);
			}
			// 执行保存文件
			File dest = new File(parentDir + "/marriage/" + filename + suffix);
			file.transferTo(dest);
			blindDateServiceBiz.savePicture(filename, originalFilename, file.getSize(), suffix);
			response.setPicId(filename);
			response.setPicUrl("/web2019/upload/marriage/" + filename + suffix);
			response.setCode("0000");
			response.setMessage("上传成功");
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			response.setMessage(e.getMessage());
			response.setCode("9999");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return response;
	}

	@RequestMapping(value = "/personList")
	public ModelAndView userList(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		modelAndView.setViewName("show/person-list");
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping("/ajaxQueryPersonList")
	@ResponseBody
	public PersonListQueryResponse queryPersonList(PersonListQueryRequest request) {
		PersonListQueryResponse response = new PersonListQueryResponse();
		VUserMarriageExample example = new VUserMarriageExample();
		VUserMarriageExample.Criteria criteria = example.createCriteria();
		if (request.getProvinceCode() != null) {
			criteria.andProvinceCodeEqualTo(request.getProvinceCode());
		}
		if (request.getEparchyCode() != null) {
			criteria.andEparchyCodeEqualTo(request.getEparchyCode());
		}
		if (StringUtils.isNotEmpty(request.getSex())) {
			criteria.andSexEqualTo(request.getSex());
		}
		if (StringUtils.isNotEmpty(request.getWeight())) {
			criteria.andWeightEqualTo(request.getWeight());
		}
		criteria.andIsDeletedEqualTo(IsDeletedConstants.IN_USE);
		PageData<VUserMarriage> data = commonServiceBiz.getPageData(example);
		response.setCode(CodeConstants.SUCCESS);
		response.setMessage("查询成功");
		response.setData(data.getData());
		response.setTotal(data.getTotal());
		return response;
	}

	@RequestMapping("/ajaxDeletePerson")
	@ResponseBody
	public DeleteStoryBudgetResponse deleteStoryBudget(HttpServletRequest request,
			DeletePersonRequest deletePersonRequest) {
		DeleteStoryBudgetResponse response = new DeleteStoryBudgetResponse();
		try {
			blindDateServiceBiz.deletePerson(request, deletePersonRequest);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			response.setMessage(e.getMessage());
			response.setCode("9999");
		}
		return response;
	}

}
